package com.yoke.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.yoke.common.OrderBaseControl;
import com.yoke.service.order.OrderService;
import com.yoke.service.order.ProductService;
import com.yoke.system.log.SystemLog;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * Created by jiangzeyin on 2017/5/17.
 */
@Controller
public class ListControl extends OrderBaseControl {
//    @Resource
//    BaseService baseService;

    @Resource
    ProductService productService;

    @Resource
    OrderService orderService;

    /**
     * @return
     */
    @RequestMapping(value = "list.html", produces = MediaType.TEXT_HTML_VALUE)
    public String list(String productName) {
        productName = convertFilePath(productName);
        JSONObject jsonOrder = orderService.getProductColumn(userName, productName);
        if (jsonOrder == null)
            return "order/list_data";
        request.setAttribute("columns", jsonOrder.getJSONArray("columns"));
        //request.setAttribute("name", name);
        request.setAttribute("product", productName);
        return "list/list";
    }


    @RequestMapping(value = "list_data.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String listData(String product, String pageNo, String pageSize, String type) {
        product = convertFilePath(product);
        return orderService.doSelect(userName, product, pageNo, pageSize, type, 0);
    }

    @RequestMapping(value = "ad_type.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String getAd(String product) {
        product = convertFilePath(product);
        return orderService.getAdType(userName, product);
    }

    /**
     * 作废
     *
     * @param product
     * @param id
     * @return
     */
    @RequestMapping(value = "zf.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String listData(String product, String id) {
        product = convertFilePath(product);
        String tableName = getTableName(product);
        return orderService.zf(tableName, id, 1);
    }

    private String getTableName(String product) {
        JSONObject jsonObject;
        try {
            jsonObject = productService.getUserProductJsonObj(userName, product);
        } catch (IOException e) {
            SystemLog.ERROR().error("获取表名", e);
            return null;
        }
        if (jsonObject == null)
            return null;
        return jsonObject.getString("tableName");
    }
}
